﻿using System.Data.Common;
using System.Data.SqlClient;
using Sx.Utils;
namespace Sx.Data
{
	public partial class DbContext
	{
		public IDbContext ConnectionString(string connectionString, IDbProvider provider)
		{ 
            
            DbProviderFactory adoNetProvider = provider.ProviderFactory;
			return ConnectionString(connectionString, provider, adoNetProvider);
		}

		public IDbContext ConnectionString(string connectionString, IDbProvider provider, DbProviderFactory adoNetProviderFactory)
		{
			Data.ConnectionString = connectionString;
			Data.FluentDataProvider = provider;
			Data.AdoNetProvider = adoNetProviderFactory;
			return this;
		}

		public IDbContext ConnectionStringName(string connectionstringName, IDbProvider dbProvider)
		{
			var connectionstring = ConfigHelper.GetConn(connectionstringName);
			if(connectionstring == null)
				throw new FluentDataException("A connectionstring with the specified name was not found in the .config file");
			
			ConnectionString(connectionstring, dbProvider);
			return this;
		}
	}
}
